package com.bibliograph.client;

import com.bibliograph.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.http.client.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BiblioGraph implements EntryPoint {
	
	
	private DockPanel dockPanel = new DockPanel();
	private VerticalPanel verticalPanel = new VerticalPanel();
	
	//panel vertical: 
	private Label lblBuscar = new Label("Buscar:");
	private ListBox cbTipoBusqueda = new ListBox();
	private TextBox textBoxBusqueda = new TextBox();
	private Button btnBuscar = new Button("Buscar");

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
			
		dockPanel.setSize("687px", "474px");
		verticalPanel.setHeight("146px");
		
		verticalPanel.add(lblBuscar);
		
		verticalPanel.add(cbTipoBusqueda);
		verticalPanel.add(textBoxBusqueda);
		
		verticalPanel.add(btnBuscar);		
		
		dockPanel.add(verticalPanel, DockPanel.WEST);
		
		RootPanel.get("panel").add(dockPanel,0,0);
		
		
		//Listeners
		btnBuscar.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String busqueda = textBoxBusqueda.getText();
				String[] busquedaSP = busqueda.split(" ");
				String urlRequest = "http://ieeexplore.ieee.org/search/searchresult.jsp?newsearch=true&queryText=" + busquedaSP[0];
				for(int i = 1; i < busquedaSP.length; i++){
					urlRequest += "+" + busquedaSP[i];
				}
				lblBuscar.setText(urlRequest);
				RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(urlRequest));

				try {
				  Request request = builder.sendRequest(null, new RequestCallback() {
				    public void onError(Request request, Throwable exception) {
				       lblBuscar.setText("error 1");
				    }

				    public void onResponseReceived(Request request, Response response) {
				      if (200 == response.getStatusCode()) {
				    	  lblBuscar.setText(response.getText());
				      } else {
				    	  lblBuscar.setText(" " + response.getStatusCode());
				      }
				    }
				  });
				} catch (RequestException e) {
					lblBuscar.setText("error 3");
				}
			}
		});
	}
		
}
